package com.example.basic.utils;

import java.util.Calendar;

public class GetAgeByBirth {

    /**
     * 根据出生日期计算年龄
     * 满一岁,只显示岁 例:1岁
     * 年龄不满一岁的,显示月份+天数
     * 分数形式  例:1 12/30  一个月又12天   天数写死30天
     * @param birthTimeString
     * @return
     */
    public static String getAgeFromBirthTime(String birthTimeString) {

        // 先截取到字符串中的年、月、日
        String[] strs = birthTimeString.trim().split("-");
        int selectYear = Integer.parseInt(strs[0]);
        int selectMonth = Integer.parseInt(strs[1]);
        int selectDay = Integer.parseInt(strs[2]);
        // 得到当前时间的年、月、日
        Calendar cal = Calendar.getInstance();
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH) + 1;
        int dayNow = cal.get(Calendar.DATE);
        // 用当前年月日减去生日年月日
        int yearMinus = yearNow - selectYear;
        int monthMinus = monthNow - selectMonth;
        int dayMinus = dayNow - selectDay;
        //本月天数
        //update by zhangzl
        //计算病人年龄,月天数写死为30
        int currentMaxDays = 30;
        String age = null;
        if(yearMinus <= 0){
            if(monthMinus <= 0){
                age = dayMinus+"天";
            }else{
                //相差天数足月
                int day = currentMaxDays - selectDay + dayNow;
                if(day == currentMaxDays){
                    age = monthMinus + "月";
                    //天数>月天数
                }else if(day > currentMaxDays){
                    age = monthMinus +" "+dayMinus+"/"+currentMaxDays+"月";
                }else{
                    //月份相差大于一月
                    if(monthMinus - 1 > 0){
                        age = (monthMinus-1) +" "+day+"/"+currentMaxDays+"月";
                    }else{
                        //天数不足月
                        age = day+"天";
                    }

                }

            }
        }else if(yearMinus >= 2 ){
            //计算相差月份
            int month = 12 - selectMonth + monthNow;
            //相差月份大于一年
            if(month > 12){
                age = yearMinus+"岁";
                //等于一年
            }else if( month == 12){
                //月份满年  只返回岁
                if(dayMinus >= 0){
                    age = yearMinus+"岁";
                }else{
                    age = (yearMinus-1) +"岁";
                }
            }else{
                age = (yearMinus-1) +"岁";
            }
        }else {
            //计算相差月份
            int month = 12 - selectMonth + monthNow;
            //相差月份大于一年
            if(month > 12){
                age = yearMinus+"岁";
                //等于一年
            }else if( month == 12){
                //月份满年  只返回岁
                if(dayMinus >= 0){
                    age = yearMinus+"岁";
                }else{
                    //月份不满年 需计算天数
                    int day = currentMaxDays - selectDay + dayNow;
                    age = (month-1) + " "+day+"/"+currentMaxDays+"月";
                }
            }else{
                //计算天数
                //相差天数大于0
                if(dayMinus > 0){
                    age = month + " " +dayMinus+"/"+currentMaxDays+"月";
                    //天数为0 只返回月份
                } else if(dayMinus == 0){
                    age = month +"月";
                }else{
                    //天数不为0, 计算相差天数
                    int day = currentMaxDays - selectDay + dayNow;
                    age = (month-1) + " "+day+"/"+currentMaxDays+"月";
                }
            }
        }
        return age;
    }
}
